home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / zoomsearch / zoomsearch.exe / {app} / highlight.js next >
Text File  |  2005-03-09  |  7KB  |  221 lines

  1. // ----------------------------------------------------------------------------
  2. // Zoom Search Engine 4.0 (9/3/2005)
  3. // Highlight & auto-scroll script
  4. //
  5. // email: zoom@wrensoft.com
  6. // www: http://www.wrensoft.com
  7. //
  8. // Copyright (C) Wrensoft 2000-2005
  9. // ----------------------------------------------------------------------------
  10. // Use this script to allow your search matches to highlight and scroll to
  11. // the matched word on the actual web page where it was found.
  12. //
  13. // You will need to link to this JS file from each page of your site
  14. // which requires the "highlight/jump to matched word" feature.
  15. //
  16. // For example, you could paste the following HTML in your site's header or 
  17. // footer:
  18. //
  19. //   <style>.highlight { background: #FFFF40; }</style>
  20. //   <script type="text/javascript" src="highlight.js"></script>
  21. //
  22. // Note: You will need to specify the correct path to "highlight.js" depending
  23. // on where the file is located.
  24. //
  25. // You will then need to modify the BODY tag on your page to include an "onLoad" 
  26. // attribute, such as:
  27. //
  28. //   <body onload="highlight();">
  29. //
  30. // If for some reason you can not modify the body tag of your page, an alternative
  31. // would be to put the following line after the </body> tag of your page:
  32. //
  33. //   <script type="text/javascript">highlight();</script>
  34. //
  35. // For more information, consult the Users Guide and our support website at:
  36. // http://www.wrensoft.com/zoom/support
  37. //
  38. //
  39. // This script is licensed for use with the Zoom Search Engine. Original
  40. // development by Brett Alcock, VBasys Limited. To licence other applications 
  41. // email highlight@vbasys.com with the subject "license".
  42.  
  43. var CatchJSErrors = true;
  44.  
  45. function catcherror() { return true; }
  46. if (CatchJSErrors)
  47. {
  48.     window.onerror = catcherror;
  49. }
  50.  
  51. function QueryString(key)
  52. {
  53.     var value = null;
  54.     for (var i=0;i<QueryString.keys.length;i++)
  55.     {
  56.         if (QueryString.keys[i]==key)
  57.         {
  58.             value = QueryString.values[i];
  59.             break;
  60.         }
  61.     }
  62.     return value;
  63. }
  64.  
  65. function QueryString_Parse()
  66. {
  67.     var query = window.location.search.substring(1);
  68.     var pairs = query.split("&");
  69.  
  70.     for (var i=0;i<pairs.length;i++)
  71.     {
  72.         var pos = pairs[i].indexOf('=');
  73.         if (pos >= 0)
  74.         {
  75.             var argname = pairs[i].substring(0,pos);
  76.             var value = pairs[i].substring(pos+1);
  77.             QueryString.keys[QueryString.keys.length] = argname;
  78.             QueryString.values[QueryString.values.length] = value;
  79.         }
  80.     }
  81. }
  82.  
  83. QueryString.keys = new Array();
  84. QueryString.values = new Array();
  85.  
  86. QueryString_Parse();
  87.  
  88. function getElement(id)
  89. {
  90.     if (document.getElementById)
  91.         return(document.getElementById(id));
  92.     else if (document.all)
  93.         return(document.all[id]);
  94. }
  95.  
  96. function findPosY(obj)
  97. {
  98.     var curtop = 0;
  99.     if (obj.offsetParent)
  100.     {
  101.  
  102.         while (obj.offsetParent)
  103.         {
  104.             curtop += obj.offsetTop
  105.             obj = obj.offsetParent;
  106.         }
  107.     }
  108.     else if (obj.y)
  109.         curtop += obj.y;
  110.     return curtop;
  111. }
  112.  
  113.  
  114. // regular expression version
  115. function SearchHiLite(text)
  116. {
  117.     var SearchAsSubstring = 0;
  118.     var hl;
  119.  
  120.     hl = QueryString("zoom_highlight");
  121.     if (hl == "" || hl == null)
  122.     {
  123.         hl = QueryString("zoom_highlightsub");
  124.         if (hl == "" || hl == null)
  125.             return false;
  126.         else
  127.             SearchAsSubstring = 1;
  128.     }
  129.     hl = unescape(hl);
  130.     hl = hl.toLowerCase();
  131.  
  132.     var term = hl.split("+"); // create array of terms
  133.  
  134.     // convert terms in regexp patterns
  135.     for (var i=0;i<term.length;i++) // take each term in turn
  136.     {
  137.         if(term[i] != "")
  138.         {
  139.             term[i]=term[i].replace(/\"/g,"");
  140.  
  141.             if (term[i].indexOf("*") != -1 || term[i].indexOf("?") != -1)
  142.             {
  143.                 // convert wildcard pattern to regexp
  144.                 term[i] = term[i].replace(/\\/g, " ");
  145.                 term[i] = term[i].replace(/\^/g, " ");
  146.  
  147.                 //term[i] = term[i].replace(/\+/g, " "); // split on this so no point in looking
  148.  
  149.                 term[i] = term[i].replace(/\#/g, " ");
  150.                 term[i] = term[i].replace(/\$/g, " ");
  151.                 term[i] = term[i].replace(/\./g, " ");
  152.                 term[i] = term[i].replace(/\*/g, "[^\\sò]*");
  153.                 term[i] = term[i].replace(/\?/g, "[^\\sò]"); // insist upon one non whitespace
  154.             }
  155.  
  156.             if (SearchAsSubstring == 0)
  157.             {
  158.                 term[i] = "(>|>[^<ò]+\\b)("+term[i]+")(\\b[^>]*<)";
  159.             }
  160.             else
  161.             {
  162.                 // if term leads with wildcard then allow it to match preceeding text in word
  163.                 var strWB="";
  164.                 if(term[i].substr(0,7)=="[^\\sò]*") strWB="\\b";
  165.                 term[i] = "(>|>[^<ò]+)"+strWB+"("+term[i]+")([^>]*<)";
  166.             }
  167.         }
  168.     }
  169.  
  170.     text=text.replace(/&/ig, '&');
  171.     text=text.replace(/ /ig, 'à');
  172.  
  173.     for (var i=0;i<term.length;i++) // take each term in turn
  174.     {
  175.         if(term[i] != "")
  176.         {
  177.             // we need a loop for the main search to catch all between ><
  178.             // and we add ò before each found to ignore those done etc
  179.             // todo: develop reliable single pass regexp and dispose of loop
  180.             var l=0;
  181.             re = new RegExp(term[i], "gi");
  182.             var count=0; // just incase
  183.             do {
  184.                 l=text.length;
  185.                 text=text.replace(re, '$1<span class="highlight" id="highlight" name="highlight">ò$2</span id="highlight">$3');
  186.                 count++;
  187.             }
  188.             //while(re.lastIndex>0 && count<100); lastIndex not set properly under netscape
  189.             while(l!=text.length && count<100);
  190.         }
  191.     }
  192.     text=text.replace(/ò/g, '');
  193.     text=text.replace(/à/g, ' ');
  194.     return(text);
  195. }
  196.  
  197. function jumpHL()
  198. {
  199.     var d=getElement("highlight");
  200.     if(d)
  201.     {
  202.         var y=findPosY(d);
  203.         // if element near top of page
  204.         if(y < 100)
  205.             window.scrollTo(0,0); // go to top of page
  206.         else
  207.             window.scrollTo(0,y-50); // show space of 50 above
  208.     }
  209. }
  210.  
  211. function highlight()
  212. {
  213.     var x = document.body;
  214.     if (x)
  215.     {
  216.         var strHTML=SearchHiLite(x.innerHTML);
  217.         if (strHTML!=false) x.innerHTML = strHTML;
  218.         jumpHL();
  219.     }
  220. }
  221.